package test21.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTCP03Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket accept = serverSocket.accept();
        InputStream inputStream = accept.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String data = "";
        while ((data = bufferedReader.readLine()) != null){
            System.out.println(data);
        }

        //使用字符输出流的方式回复信息
        OutputStream outputStream = accept.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello client 字符流");
        // 插入一个换行符，表示回复内容的结束
        bufferedWriter.newLine();
        //注意需要手动的flush
        bufferedWriter.flush();

        //关闭流和socket
        bufferedWriter.close();
        bufferedReader.close();
        accept.close();
        serverSocket.close();//关闭
    }
}
